package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.DevChangeRecordDao;
import com.sugon.dao.DevLeaveRecordDao;
import com.sugon.dao.DeviceInfoDao;
import com.sugon.entity.DevChangeRecordEntity;
import com.sugon.entity.DevLeaveRecordEntity;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.service.DevLeaveRecordService;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 设备闲置记录Service实现类
 *
 * @author sugon
 * @date 2019-07-12 08:49:29
 */
@Service("devLeaveRecordService")
public class DevLeaveRecordServiceImpl implements DevLeaveRecordService {
    @Autowired
    private DevLeaveRecordDao devLeaveRecordDao;
    @Autowired
    private DeviceInfoDao deviceInfoDao;
    @Autowired
    private DevChangeRecordDao devChangeRecordDao;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevLeaveRecordEntity queryObject(String id) {
        return devLeaveRecordDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevLeaveRecordEntity> queryList(Map<String, Object> map) {
        return devLeaveRecordDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devLeaveRecordDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param devLeaveRecord 实体
     * @return
     */
    @Override
    public int save(DevLeaveRecordEntity devLeaveRecord) {
        // 保存设备闲置记录
        devLeaveRecord.setId(RandomUtil.randomString(32));
        devLeaveRecord.setCreateDate(new Date());
        devLeaveRecord.setCreateUser(getUserId());
        devLeaveRecord.setDelFlag(Constant.IS_NOT_DEL);
        return devLeaveRecordDao.save(devLeaveRecord);
    }

    /**
     * update
     *
     * @param devLeaveRecord 实体
     * @return
     */
    @Override
    public int update(DevLeaveRecordEntity devLeaveRecord) {
        // 更新设备闲置记录
        devLeaveRecord.setUpdateDate(new Date());
        devLeaveRecord.setUpdateUser(getUserId());
        return devLeaveRecordDao.update(devLeaveRecord);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devLeaveRecordDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devLeaveRecordDao.deleteBatch(ids);
    }

    /**
     * review
     *
     * @param devLeaveRecord
     * @return
     */
    @Override
    public int review(DevLeaveRecordEntity devLeaveRecord) {
        return devLeaveRecordDao.update(devLeaveRecord);
    }

    /**
     * updateStatus
     *
     * @param id     闲置记录ID
     * @param status 状态
     * @return
     */
    @Override
    public boolean updateStatus(String id, String status) {
        DevLeaveRecordEntity leaveRecord = new DevLeaveRecordEntity();
        leaveRecord.setId(id);
        leaveRecord.setStatus(status);
        leaveRecord.setUpdateDate(DateUtil.date());
        leaveRecord.setUpdateUser(ShiroUtils.getUserId());
        return devLeaveRecordDao.update(leaveRecord) > 0;
    }

    /**
     * remove
     *
     * @param list
     * @return
     */
    @Override
    public int remove(List<DevLeaveRecordEntity> list) {
        // 过滤掉设备状态为空的
        list = list.stream().filter(leaveRecord->
            StrUtil.isNotEmpty(leaveRecord.getStatus())
        ).collect(Collectors.toList());
        // 更新设备闲置记录
        List<DeviceInfoEntity> deviceInfoEntityList = new ArrayList<>();
        String[] ids = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            DevLeaveRecordEntity entity = list.get(i);
            DeviceInfoEntity deviceInfoEntity = new DeviceInfoEntity();
            deviceInfoEntity.setId(entity.getDevId());
            ids[i] = entity.getDevId();
            deviceInfoEntity.setDevStatus(entity.getStatus());
            deviceInfoEntityList.add(deviceInfoEntity);
        }
        //根据设备id 查询设备信息list
        List<DeviceInfoEntity> infoList = deviceInfoDao.getDevInfo(ids);
        //删除闲置记录
        devLeaveRecordDao.remove(list);
        //添加台账记录
        List<DevChangeRecordEntity> changeList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DevLeaveRecordEntity entity = list.get(i);
            if (i >= infoList.size()) {
                break;
            }
            DeviceInfoEntity info = infoList.get(i);
            DevChangeRecordEntity changeRecordEntity = new DevChangeRecordEntity();
            changeRecordEntity.setId(RandomUtil.randomString(32));
            changeRecordEntity.setDevName(entity.getDevName());
            changeRecordEntity.setDevCode(entity.getDevCode());
            changeRecordEntity.setOrigVal(entity.getOrigVal());
            changeRecordEntity.setNetVal(entity.getNetVal());
            changeRecordEntity.setDevModel(entity.getDevModel());
            changeRecordEntity.setReson("闲置移除记录");
            changeRecordEntity.setUseUnit(entity.getUseUnit());
            changeRecordEntity.setDevStatus(Integer.valueOf(entity.getStatus()));
            changeRecordEntity.setAddress(entity.getAddress());
            changeRecordEntity.setDevNum(entity.getDevNum());
            changeRecordEntity.setDevProductName(info.getDevProductName());
            changeRecordEntity.setDevProceNo(info.getDevProceNo());
            changeRecordEntity.setDevProceName(info.getDevProceName());
            changeRecordEntity.setWorkshop(info.getDevFarm());
            changeRecordEntity.setCreateDate(new Date());
            changeRecordEntity.setCreateUser(ShiroUtils.getUserId());
            changeList.add(changeRecordEntity);
        }
        //变更(台账记录)添加
        devChangeRecordDao.addChangeRecordList(changeList);

        //修改设备清单
        return deviceInfoDao.batchUpdate(deviceInfoEntityList);
    }
}
